package com.sansecy.customview.device;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Parcel;
import android.os.RemoteException;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.sansecy.customview.R;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class DeviceActivity extends AppCompatActivity {
    public static void start(Context context) {
        Intent starter = new Intent(context, DeviceActivity.class);
//    starter.putExtra();
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device);
        View openBtn = findViewById(R.id.btn_open);
        openBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openDebugLayoutOptions(true);
            }
        });
        View closeBtn = findViewById(R.id.btn_close);
        closeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openDebugLayoutOptions(false);
//                try {
//                    Class<?> aClass = Class.forName("android.os.SystemProperties");
//                    Method get = aClass.getMethod("get", String.class);
//                    Object getInvoke = get.invoke(aClass, "debug.layout");
//                    Method set = aClass.getMethod("set", String.class, String.class);
//                    Object invoke = set.invoke(aClass, "debug.layout", "true");
//                    pokeSystemProperties();
//                } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
//                }
            }
        });
//        String s = GetKernel.get("debug.layout");
//        Log.d(this.getClass().getName()+"-App", "onCreate:s " + s);

        Button btnSimulateSecondaryDisplays = findViewById(R.id.btn_simulate_secondary_displays);
        btnSimulateSecondaryDisplays.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Settings.Global.putString(getContentResolver(), "overlay_display_devices", "1280x720/213");
            }
        });
    }

//      <string-array name="overlay_display_devices_values" translatable="false" >
//        <item></item>
//        <item>720x480/160</item>
//        <item>1280x720/213</item>
//        <item>1920x1080/320</item>
//        <item>1280x720/213;1920x1080/320</item>
//    </string-array>

    //    private void openDebugLayoutOptions() {
//
//        SystemProperties.set("debug.layout", str);
//        pokeSystemProperties();
//    }


    private void openDebugLayoutOptions(boolean openDebugLayout) {
        try {
            Class<?> aClass = Class.forName("android.os.SystemProperties");
            Method set = aClass.getMethod("set", String.class, String.class);
            set.invoke(aClass, "debug.layout", String.valueOf(openDebugLayout));
        } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        pokeSystemProperties();
    }

    void pokeSystemProperties() {
//        try {
//            Class<?> localClass = Class.forName("android.os.ServiceManager");
//            Method listServices = localClass.getMethod("listServices");
//            String[] arrayOfString = (String[]) listServices.invoke(localClass);
//            Log.d(this.getClass().getName()+"-App", "pokeSystemProperties: " + Arrays.toString(arrayOfString));
//            Log.d(this.getClass().getName()+"-App", "pokeSystemProperties: " + arrayOfString.length);
////            if (result != null) {
////                IBinder binder = (IBinder) result;
////                // 无论您要在何时进行此操作，都需要将绑定设置为所使用服务的正确类型。
////            }
//        } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
//            StringBuilder stringBuilder = new StringBuilder();
//            StackTraceElement[] stackTrace = e.getStackTrace();
//            for (StackTraceElement stackTraceElement : stackTrace) {
//                stringBuilder.append(stackTraceElement).append("\n");
//            }
//            Log.d(this.getClass().getName()+"-App", "onClick:debug.layout getLocalizedMessage  " + e.getLocalizedMessage());
//            Log.d(this.getClass().getName()+"-App", "onClick:debug.layout " + stringBuilder.toString());
//            Toast.makeText(DeviceActivity.this, Arrays.toString(e.getStackTrace()), Toast.LENGTH_LONG).show();
//        }
        (new SystemPropPoker()).execute();
    }

    static class SystemPropPoker extends AsyncTask<Void, Void, Void> {
        protected Void doInBackground(Void... param1VarArgs) {
            IBinder iBinder;
            byte b = 0;
            String str;
            try {
                Class<?> localClass = Class.forName("android.os.ServiceManager");
                Method listServices = localClass.getMethod("listServices");
                Method checkService = localClass.getMethod("checkService", String.class);

                String[] arrayOfString = (String[]) listServices.invoke(localClass);
                int i = arrayOfString.length;
                while (b < i) {
                    str = arrayOfString[b];
                    iBinder = (IBinder) checkService.invoke(localClass, str);
                    if (iBinder != null) {
                        Parcel parcel = Parcel.obtain();
                        try {
                            iBinder.transact(1599295570, parcel, null, 0);
                        } catch (RemoteException e) {
                            Log.d(this.getClass().getName() + "-App", "doInBackground: " + e);
                        } catch (Exception e) {
                            Log.d(this.getClass().getName() + "-App", "doInBackground: " + e.getMessage());
                            Log.i("DevelopmentSettings", "Someone wrote a bad service '" + str + "' that doesn't like to be poked: " + iBinder);
                        }
                        parcel.recycle();
                    }
                    b++;
                }
            } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | ClassNotFoundException e) {
                Log.d(this.getClass().getName() + "-App", "doInBackground: " + e.getMessage());
                return null;
            }
            return null;
        }
    }
}
